﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using WholeSlideImageLibrary;

namespace WholeSlideImageViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : IMainWindowInteraction
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly IAddressService _addressService;
        private readonly MainWindowViewModel _viewModel;

        public MainWindow(IServiceProvider serviceProvider, IAddressService addressService, IDeepZoomImageSource deepZoomImageSource, IExportService exportService)
        {
            InitializeComponent();

            _viewModel = new MainWindowViewModel(serviceProvider, this);
            DataContext = _viewModel;

            _serviceProvider = serviceProvider;
            _addressService = addressService;
        }

        public void NavigateWebView(string url)
        {
            webBrowser1.Navigate(url);
        }

        public string? ShowOpenFileDialog()
        {
            var dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.DereferenceLinks = true;
            dialog.DefaultExt = ".tif";
            dialog.Filter = "所有支持的类型|*.tif;*.tiff;*.svs;*.vms;*.vmu;*.ndpi;*.scn;*.mrxs;*.svslide;*.bif;*.dmetrix;*.opti;*.dzi;*.bmp;*.png;*.jpg;*.jpeg;*.tga|Aperio (*.svs;*.tif)|*.svs;*.tif|Hamamatsu (*.vms;*.vmu;*.ndpi)|*.vms;*.vmu;*.ndpi|Leica (*.scn)|*.scn|MIRAX (*.mrxs)|*.mrxs|Philips (*.tiff)|*.tiff|Sakura (*.svslide)|*.svslide|Trestle (*.tif)|*.tif|Ventana (*.bif;*.tif)|*.bif;*.tif|Generic tiled TIFF (*.tif;*.tiff)|*.tif;*.tiff|Deep Zoom (*.dzi)|*.dzi|Dmetrix (*.dmetrix)|*.dmetrix|Optimized Pyramid Tile Image (*.opti)|*.opti|图片文件 (*.bmp;*.png;*.jpg;*.jpeg;*.tga)|*.bmp;*.png;*.jpg;*.jpeg;*.tga|所有文件(*.*)|*.*";
            bool? ret = dialog.ShowDialog(this);
            return ret.GetValueOrDefault() ? dialog.FileName : null;
        }

        public string? ShowExportTiffFileDialog()
        {
            var dialog = new SaveFileDialog();
            dialog.OverwritePrompt = true;
            dialog.DefaultExt = ".tif";
            dialog.Filter = "TIFF 文件|*.tif;*.tiff"; ;
            bool? ret = dialog.ShowDialog(this);
            return ret.GetValueOrDefault() ? dialog.FileName : null;
        }

        public string? ShowExportOptiFileDialog()
        {
            var dialog = new SaveFileDialog();
            dialog.OverwritePrompt = true;
            dialog.DefaultExt = ".opti";
            dialog.Filter = "OPTI 文件|*.opti|TIFF 文件|*.tif;*.tiff"; ;
            bool? ret = dialog.ShowDialog(this);
            return ret.GetValueOrDefault() ? dialog.FileName : null;
        }

        public void AlertServerLoadFailure()
        {
            MessageBox.Show("服务启动失败。");
        }

        public void AlertSlideLoadFailure(Exception? ex)
        {
            if (ex is null)
            {
                MessageBox.Show("加载失败。");
            }
            else
            {
                MessageBox.Show("加载失败。" + ex.Message);
            }
        }

        public ITiffOutputOptions? ShowTiffExportOptions()
        {
            TiffOutputDialog? optionsWindow = _serviceProvider.GetService<TiffOutputDialog>();
            optionsWindow.ShowDialog();
            return optionsWindow.OutputOptions;
        }

        public void ShowTiffExportDialog(IExportProgress progress, CancellationTokenSource cts)
        {
            var progressDialog = new ExportProgressDialog(progress, cts);
            progressDialog.ShowDialog();
        }

        public void AlertTiffExportFailure(Exception? ex)
        {
            if (ex is null)
            {
                MessageBox.Show("导出失败。");
            }
            else
            {
                MessageBox.Show("导出失败。" + ex.Message);
            }
        }

        public IOptiOutputOptions? ShowOptiExportOptions()
        {
            OptiOutputDialog? optionsWindow = _serviceProvider.GetService<OptiOutputDialog>();
            optionsWindow.ShowDialog();
            return optionsWindow.OutputOptions;
        }

        public void ShowOptiExportDialog(IExportProgress progress, CancellationTokenSource cts)
        {
            var progressDialog = new ExportProgressDialog(progress, cts);
            progressDialog.ShowDialog();
        }

        public void AlertOptiExportFailure(Exception? ex)
        {
            if (ex is null)
            {
                MessageBox.Show("导出失败。");
            }
            else
            {
                MessageBox.Show("导出失败。" + ex.Message);
            }
        }

        public void AlertShowPropertiesFailure(Exception? ex)
        {
            if (ex is null)
            {
                MessageBox.Show("获取属性失败。");
            }
            else
            {
                MessageBox.Show("获取属性失败。" + ex.Message);
            }
        }

        public void ShowPropertiesDialog(IEnumerable<KeyValuePair<string, string>> properties)
        {
            TextPropertiesDialog dialog = _serviceProvider.GetService<TextPropertiesDialog>();
            dialog.SetViewData(properties.Select(i => new TextPropertiesDialogPropertyItem(i.Key, i.Value)));
            dialog.ShowDialog();
        }

        public void AlertShowAssociatedImagesFailure(Exception? ex)
        {
            if (ex is null)
            {
                MessageBox.Show("获取附加图片失败。");
            }
            else
            {
                MessageBox.Show("获取附加图片失败。" + ex.Message);
            }
        }

        public void ShowAssociatedImagesDialog(IEnumerable<string> names)
        {
            if (!_addressService.TryGetAccessAddress(out Uri? uri))
            {
                return;
            }

            AssociatedImageDialog dialog = _serviceProvider.GetService<AssociatedImageDialog>();
            dialog.SetViewData(uri.ToString(), _viewModel.Id!, names);
            dialog.ShowDialog();
        }

        public void AlertAbout()
        {
            MessageBox.Show("WholeSlideImageViewer" + Environment.NewLine + AppVersion.AssemblyInformationalVersion);
        }

        public void ApplicationExit()
        {
            Application.Current.Shutdown();
        }

    }
}
